/*
* Author:no-bald-chen
* Time:2021/8/20
* */

package DataStructure.graph;

public class DAG implements Graph{
    private int numVertex;      //顶点数目
    private int numEdge;        //边的数目
    private Vertex[] vertices;  //顶点集
    private int[][] G;          //矩阵图
    private final int MAX = 9999;      //最大权值

    public Vertex[] getVertices() {
        return vertices;
    }

    public int[][] getG() {
        return G;
    }

    public int getNumVertex() {
        return numVertex;
    }

    public int getNumEdge() {
        return numEdge;
    }

    @Override
    public void InitGraphByMatrix(Triplet[] triplets,int numVertex) {
        this.numVertex = numVertex;
        numEdge=triplets.length;
        G = new int[numVertex+1][numVertex+1];
        for (int i = 0; i <=numVertex; i++) {
            for (int j = 0; j <=numVertex; j++) {
                G[i][j] = MAX;
            }
        }

        for (Triplet triplet : triplets) {
            G[triplet.getV1()][triplet.getV2()] = triplet.getWeight();
        }
    }

    @Override
    public void InitGraphByTable(Triplet[] triplets,int numVertex) {
        vertices = new Vertex[numVertex+1];
        for (int i = 0; i <= numVertex; i++) {
            vertices[i] = new Vertex(i,0,null);
        }
        for (Triplet triplet : triplets) {
            Vertex vertex = new Vertex(triplet.getV2(), triplet.getWeight(),null);
            vertex.next = vertices[triplet.getV1()].next;
            vertices[triplet.getV1()].next = vertex;
        }
    }

    @Override
    public void TravelGraph() {
        if (vertices!=null){
            for (int i = 1; i < vertices.length; i++) {
                Vertex vertex = vertices[i];
                while (vertex!=null && !vertex.flag){
                    if (vertex!=vertices[i]){
                        System.out.println(vertices[i].number+"----->"+vertex.number+"="+vertex.weight);
                    }
                    vertex=vertex.next;
                }
            }
        }else if (G!=null){
            for (int i = 1; i <=numVertex ; i++) {
                for (int j = 1; j <=numVertex; j++) {
                    if (G[i][j]!=MAX){
                        System.out.println(i+"----->"+j+"="+G[i][j]);
                    }
                }
            }
        }
    }
}
